<?php

namespace app\modules\admin\controllers;

use app\common\controllers\AccessController;
use app\modules\admin\forms\printMachine\PrintForm;
use app\modules\admin\services\PrintService;
use yii\base\UserException;

class PrintController extends AccessController{

    //搜索
    public function actionSearch(){
        $sn = \Yii::$app->getRequest()->get("sn");
        $pagesize = \Yii::$app->getRequest()->get("pagesize");

        return [
            'list'=>PrintService::search($sn,$page,$pagesize),
            'page'=>$page->Out(),
        ];
    }

    //添加
    public function actionAdd(){
        $form = new PrintForm();
        $form->setScenario("create");
        if($form->load(\Yii::$app->getRequest()->post(),'') && PrintService::create($form)){
            return "";
        }else{
            throw new UserException(PrintService::getError());
        }
    }

    //获取信息
    public function actionGetInfoById($id){
        return PrintService::getInfoById($id);
    }

    //编辑
    public function actionEdit(){
        $form = new PrintForm();
        $form->setScenario("update");
        if($form->load(\Yii::$app->getRequest()->post(),'') && PrintService::update($form)){
            return "";
        }else{
            throw new UserException(PrintService::getError());
        }
    }

    public function actionDelete($id){
        return PrintService::delete($id);
    }


    //绑定
    public function actionBind($id,$merchant_id){
        if(PrintService::bind($id,$merchant_id)){
            return "";
        }else{
            throw new UserException(PrintService::getError());
        }
    }

    //解绑
    public function actionUnbind($id){
        if(PrintService::unbind($id)){
            return "";
        }else{
            throw new UserException(PrintService::getError());
        }
    }
}